package com.xmgong.hxdemo.hxdemo;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.hyphenate.EMCallBack;
import com.hyphenate.EMConnectionListener;
import com.hyphenate.EMError;
import com.hyphenate.chat.EMClient;
import com.hyphenate.util.NetUtils;
import com.xmgong.hxdemo.chatmodule.ChatPlugin;
import com.xmgong.hxdemo.chatmodule.pages.chat.ChatActivity;
import com.xmgong.hxdemo.chatmodule.pages.conversation.ConversationActivity;

public class MainActivity extends AppCompatActivity {

    private String userName = "gongxm";
    private String password = "123456";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        //注册一个监听连接状态的listener
        EMClient.getInstance().addConnectionListener(new MyConnectionListener());
    }

    public void RegistClick(View view) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                ChatPlugin.RegistAccount(userName, password);
            }
        }).start();
    }

    public void LoginClick(View view) {
        ChatPlugin.LoginAccount(userName, password, new EMCallBack() {
            @Override
            public void onSuccess() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(MainActivity.this, "登录Success", Toast.LENGTH_SHORT).show();
                    }
                });
                ChatPlugin.LoadChatData();
            }

            @Override
            public void onError(int code, String error) {
                Log.i("MainActivity", error);
            }

            @Override
            public void onProgress(int progress, String status) {
                Log.i("MainActivity", "登录中。。。" + progress);
                Log.i("MainActivity", "状态" + status);

            }
        });
    }

    //跳转一对一聊天
    public void ChatClick(View view) {
        Intent mintent = new Intent(MainActivity.this, ChatActivity.class);
        startActivity(mintent);
    }

    /**
     * 跳转会话列表
     * @param view
     */
    public void ConversationClick(View view) {
        Intent mintent = new Intent(MainActivity.this, ConversationActivity.class);
        startActivity(mintent);
    }


    //实现ConnectionListener接口
    private class MyConnectionListener implements EMConnectionListener {
        @Override
        public void onConnected() {
            Log.i("MainActivity", "已连接");
        }

        @Override
        public void onDisconnected(final int error) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if (error == EMError.USER_REMOVED) {
                        // 显示帐号已经被移除
                        Log.i("MainActivity", "显示帐号已经被移除");
                    } else if (error == EMError.USER_LOGIN_ANOTHER_DEVICE) {
                        // 显示帐号在其他设备登录
                        Log.i("MainActivity", "显示帐号在其他设备登录");
                    } else {
                        if (NetUtils.hasNetwork(MainActivity.this)) {
                            //连接不到聊天服务器
                            Log.i("MainActivity", "连接不到聊天服务器");
                        } else {
                            //当前网络不可用，请检查网络设置
                            Log.i("MainActivity", "当前网络不可用，请检查网络设置");
                        }
                    }
                }
            });
        }
    }

}
